<?php
/**
 * Created by PhpStorm.
 * User: xloong
 * Date: 2017/7/26
 * Time: 14:50
 */

namespace App\Http\Controllers\Api\V1;

use App\Models\Identify;
use App\Models\IdentifyPhoto;
use Illuminate\Http\Request;
class IdentifyController extends BaseController
{
    private $result = array('code' => '10000', 'message' => '系统服务异常', 'data' => array());
    public function getInfoByName(Request $request)
    {
        // 自定义验证规则
        $rules = [
            'name' => 'required|alpha_dash',
        ];
        // 自定义提示
        $messages = [
            'name.required' => ['code' => 20901, 'message' => ':attribute 不能为空.', 'data' => []],
            'name.alpha_dash' => ['code' => 20902, 'message' => ':attribute 格式不正确.', 'data' => []],
        ];
        $validator = \Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            $messages = $validator->errors()->toArray();
            foreach ($messages as $value) {
                $this->result['code'] = (int)$value[0]['code'];
                $this->result['message'] = $value[0]['message'];
                break;
            }
            return $this->response->array($this->result);
        }
        $name = $request->get('name');
        //修改后得根据识别图name查询识别信息方法改变，使用join进行两表联查
        $identify = Identify::join('identify_images','identifies.id','=','identify_images.identify_id')
                    ->where('identify_images.identify_name',e($name))->first();
        //识别信息为空
        if (empty($identify)) {
            $this->result['code'] = 20903;
            $this->result['message'] = '没有对应识别信息';
            return $this->response->array($this->result);
        }

        //获取识别信息对应图文信息
        $imageId = json_decode($identify->photo_id, true);
        if (!empty($imageId)) {
            $photos = IdentifyPhoto::whereIn('id', $imageId)->select('title', 'content', 'url')->get();
        } else {
            $photos = [];
        }
        //获取识别信息对应视频信息
        $video = $identify->getVideo;
        //获取识别信息发布者
        $username = $identify->getUser->username;
        //封装识别视频信息
        $videos = empty($video) ? (object)[] : ['title' => $video->title, 'url' => $video->url];
        //封装返回信息
        $datas = ['writer' => $username, 'is_red_packets' => $identify->is_red_packets,
            'photo' => $photos, 'video' => $videos,'identify_id'=>$identify->identify_id,
            'shop_address'=>empty($identify->shop_address)?config('tools.shop_address'):$identify->shop_address,
            'red_packets_total' => count(json_decode($identify->red_packets, true)),
            'share_url'=>$identify->share_url];
        //判断返回得type类型，2为只有图片，3为只有视频，4既有视频又有图片,1为只有图文信息
        if (is_object($videos) && !empty($photos)) {
            $datas['type'] = 1;
        } elseif (empty($photos) && !is_object($videos)) {
            $datas['type'] = 2;
        } elseif (!empty($photos) && !is_object($videos)) {
            $datas['type'] = 3;
        } else {
            $datas['type'] = '';
        }
        $this->result['code'] = 200;
        $this->result['message'] = 'ok';
        $this->result['data'] = $datas;
        return $this->response->array($this->result);
    }
}